<?php
class MRequest {
    private $_url;
    private $_params;
    private $_module;
    private $_controller;
    private $_action;
    private $_isAdmin = false;

    public function  __construct() {
        $this->_url = $this->_getCurrentUrl();
    }

    public function setUrl($url){
        $this->_url = $url;
    }

    /**
     * @return String : The URL of the Request
     */
    public function getUrl(){
        return $this->_url;
    }

    /**
     * @return Array : The parameters of the request
     */
    public function getParams(){
        return $this->_params;
    }

    public function setParams($params){
        $this->_params = $params;
    }

    /**
     * @return Array : All the parameters of the POST method
     */
    public function getPost(){
        return $this->_params['post'];
    }

    /**
     * @return Array : All the parameters of the GET method
     */
    public function getGet(){
        return $this->_params['get'];
    }

    /**
     * @return String : The current URL
     */
    private function _getCurrentUrl() {
        return 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
    }

    /**
     * @return String : The module called in the request
     */
    public function getModule() {
        return $this->_module;
    }

    public function setModule($_module) {
        $this->_module = $_module;
    }

    /**
     * @return String : The controller called in the request
     */
    public function getController() {
        return $this->_controller;
    }

    public function setController($_controller) {
        $this->_controller = $_controller;
    }

    /**
     * @return String : The action called in the request
     */
    public function getAction() {
        return $this->_action;
    }

    public function setAction($_action) {
        $this->_action = $_action;
    }
    
    public function isAdmin($isAdmin = FALSE) {
        if ($isAdmin !== FALSE)
            $this->_isAdmin = $isAdmin;
        return $this->_isAdmin;
    }
   
    /**
     * @return Boolean : If the request method is GET
     */
    public function isGet() {
        return !isset($_SERVER['REQUEST_METHOD']);
    }
    
    /**
     * @return Boolean : If the request method is POST
     */
    public function isPost() {
        return isset($_SERVER['REQUEST_METHOD']) && !strcasecmp($_SERVER['REQUEST_METHOD'],'POST');
    }
    
    /**
     * @return Boolean : If the request method is XMLHttpRequest
     */
    public function isAjax() {
        return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH']==='XMLHttpRequest';
    }

    /**
     * Check if the $url in parameter is the current url
     * @param string $url
     * @return boolean
     */
    public static function isCurrentUrl($url){
        $currentRequest = new MRequest();
        $requestedRequest = new MRequest();
        $requestedRequest->setUrl($url);
        MRouter::getInstance()->getRoute($currentRequest);
        MRouter::getInstance()->getRoute($requestedRequest);
        return (($currentRequest->getModule() == $requestedRequest->getModule()) && ($currentRequest->getController() == $requestedRequest->getController()) && ($currentRequest->getAction() == $requestedRequest->getAction()));
    }
    
    /**
     * Redirect the request to the specified URL
     * @param String $url : URL to redirect to
     */
    public static function redirect($url){
        header('Location: '.$url, true);
    }
}
?>
